package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerGuestPotentialDetailDTO;
import com.prd.mapper.CustomerGuestPotentialDetailMapper;
import com.prd.model.CustomerGuestPotentialDetailDO;
import com.prd.service.CustomerGuestPotentialDetailService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户-客人消费潜力详情总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-12-14
 */
@Service("CustomerGuestPotentialDetailService")
public class CustomerGuestPotentialDetailServiceImpl extends ServiceImpl<CustomerGuestPotentialDetailMapper, CustomerGuestPotentialDetailDO> implements CustomerGuestPotentialDetailService {


    @Autowired
    private CustomerGuestPotentialDetailMapper potentialDetailMapper;

    @Override
    public JsonData uUpdateAndAdd(CustomerGuestPotentialDetailDTO potentialDetailDTO) {
        LambdaQueryWrapper<CustomerGuestPotentialDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestPotentialDetailDO::getGuestNo, potentialDetailDTO.getGuestNo());
        CustomerGuestPotentialDetailDO potentialDetailDO = potentialDetailMapper.selectOne(wrapper);
        if (potentialDetailDO == null) {
            potentialDetailDO = new CustomerGuestPotentialDetailDO();
            CopyNonNullBeanUtils.copyProperties(potentialDetailDTO, potentialDetailDO);
            potentialDetailMapper.insert(potentialDetailDO);
        }
        else {
            CopyNonNullBeanUtils.copyProperties(potentialDetailDTO, potentialDetailDO);
            potentialDetailMapper.update(potentialDetailDO, wrapper);
        }
        return JsonData.buildSuccess(potentialDetailDO);
    }

    @Override
    public JsonData uSearch(String guestNo) {
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");
        return JsonData.buildSuccess(search(guestNo));
    }

    @Override
    public CustomerGuestPotentialDetailDTO search(String guestNo) {
        LambdaQueryWrapper<CustomerGuestPotentialDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestPotentialDetailDO::getGuestNo, guestNo);
        CustomerGuestPotentialDetailDO potentialDetailDO = potentialDetailMapper.selectOne(wrapper);
        CustomerGuestPotentialDetailDTO potentialDetailDTO = new CustomerGuestPotentialDetailDTO();
        if (potentialDetailDO != null)
            CopyNonNullBeanUtils.copyProperties(potentialDetailDO, potentialDetailDTO);
        return potentialDetailDTO;
    }
}
